package com.itkim.qian.controller;

import com.itkim.YZUserInfo.domain.YzUser;
import com.itkim.judge.domain.YzJudgeRes;
import com.itkim.qian.domain.YzUserRes;
import com.itkim.qian.domain.YzWorkR;
import com.itkim.qian.service.LoginService;
import com.itkim.work.controller.YzWorkController;
import com.itkim.work.domain.YzFile;
import com.itkim.work.domain.YzWork;
import com.itkim.work.domain.YzWorkRes;
import com.itkim.work.service.IYzWorkService;
import com.itkim.work.service.impl.YzWorkServiceImpl;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/user")
public class LoginController extends BaseController {

    @Autowired
    private LoginService loginService;

    /**
     * 登录
     * @param yzUserRes
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public AjaxResult login(YzUserRes yzUserRes) throws Exception {
        return loginService.login(yzUserRes);
    }

    /**
     * 修改密码
     * @param oldPassword
     * @param newPassword
     * @param yzUserRes
     * @return
     */
    @PostMapping("/update")
    public AjaxResult updatePassword(@RequestParam("oldPassword") String oldPassword,
                                     @RequestParam("newPassword") String newPassword,
                                     YzUserRes yzUserRes){
        return toAjax(loginService.updatePassword(oldPassword,newPassword,yzUserRes));
    }

    /**
     * 参赛报名
     * @param yzUser
     * @param yzWorkRes
     * @return
     */
    @PostMapping("/baoming")
    public AjaxResult baoming(YzUser yzUser, YzWorkRes yzWorkRes){
        return AjaxResult.success(loginService.baoming(yzUser.getUserId(),yzWorkRes));
    }

    /**
     * 上传作品文件
     * @param workId
     * @param file
     * @param group
     * @throws IOException
     */
    @PostMapping("/export")
    public void export(@RequestParam("workId") Long workId,
                       MultipartFile file,
                       @RequestParam("group") Integer group) throws IOException {
        loginService.export(workId,group,file);
    }

    /**
     * 评委：查询该组别的作品列表
     * @param yzWorkR
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo list(YzWorkR yzWorkR){
        startPage();
        List<YzWorkR> list = loginService.list(yzWorkR);
        return getDataTable(list);
    }

    @Autowired
    private IYzWorkService iYzWorkService;

    /**
     * 评委查看作品详情
     * @param workId
     * @return
     */
    @PostMapping("/look")
    public Map<String,Object> look(Long workId){
        YzWorkRes yzWorkRes = new YzWorkRes();
        yzWorkRes.setWorkId(workId);
        return iYzWorkService.look(yzWorkRes);
    }

    @PostMapping("/judge")
    public AjaxResult judge(YzJudgeRes yzJudgeRes){
        return toAjax(loginService.judge(yzJudgeRes));
    }
}
